package com.typhoon.spring_shiro.utils.file;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil
{
    /**
     * 生成缩略图 <br/>
     * 保存:ImageIO.write(BufferedImage, imgType[jpg/png/...], File);
     * 
     * @param source 原图片
     * @param width 缩略图宽
     * @param height 缩略图高
     * @param b 是否等比缩放
     */
    public static BufferedImage thumb( BufferedImage source, int width, int height, boolean b )
    {
        // targetW，targetH分别表示目标长和宽
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) width / source.getWidth();
        double sy = (double) height / source.getHeight();
        if ( b )
        {
            if ( sx > sy )
            {
                sx = sy;
                width = (int) ( sx * source.getWidth() );
            }
            else
            {
                sy = sx;
                height = (int) ( sy * source.getHeight() );
            }
        }

        if ( type == BufferedImage.TYPE_CUSTOM )
        { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster( width, height );
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage( cm, raster, alphaPremultiplied, null );
        }
        else
            target = new BufferedImage( width, height, type );
        Graphics2D g = target.createGraphics();
        // smoother than exlax:
        g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
        g.drawRenderedImage( source, AffineTransform.getScaleInstance( sx, sy ) );
        g.dispose();
        return target;
    }

    /**
     * 生成缩略图
     * 
     * @param fromFileStr
     * @param saveToFileStr
     * @param width
     * @param hight
     * @throws Exception
     */
    public static void saveThumbImage( String fromFileStr, String saveToFileStr, int width, int hight )
        throws Exception
    {
        BufferedImage srcImage;
        // String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
        String imgType = "JPEG";
        if ( fromFileStr.toLowerCase().endsWith( ".png" ) )
        {
            imgType = "PNG";
        }
        File saveFile = new File( saveToFileStr );
        File fromFile = new File( fromFileStr );
        srcImage = ImageIO.read( fromFile );
        if ( width > 0 || hight > 0 )
        {
            srcImage = thumb( srcImage, width, hight, true );
        }
        ImageIO.write( srcImage, imgType, saveFile );
    }

    /**
     * 获取图片宽高
     * 
     * @param fromFileStr
     * @return int[0] width int[1] height
     * @throws IOException
     */
    public static int[] getWidthAndHeight( String fromFileStr )
        throws IOException
    {
        File fromFile = new File( fromFileStr );
        BufferedImage srcImage = ImageIO.read( fromFile );
        int[] wh = new int[2];
        wh[0] = srcImage.getWidth();
        wh[1] = srcImage.getHeight();
        return wh;
    }

    public static void main( String argv[] )
    {
        try
        {
            // 参数1(from),参数2(to),参数3(宽),参数4(高)
            ImageUtil.saveThumbImage( "E:/Desktop/123.jpg", "E:/Desktop/123_thumb.jpg", 50, 50 );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
}
